﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TPFVA06
    {

        public TPFVA06()
        {

            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAART();
            this.DataContext = MV;
            InitializeComponent();

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                VAPR = "";
            }
            else
            {
                if (dwMaster["VAANEUDAT"].ToString().Trim() == "0")
                {
                    dwMaster["VAANEUDAT"] = MV.date;
                }
                VAPR = "VA" + dwMaster["VAANEUDAT"].ToString().Substring(2, 6)
                    + "OF"
                    + CURRENCY + dwMaster["VAAKOLL"].ToString() + dwMaster["VAAART"].ToString();

            }
        }

        public void UpdateByWS(MessageInfo msg)
        {
            if (dwMaster == null)
                return;
            dwMaster["VAAGEWVON"] = Convert.ToDouble(msg.P1) * 100;
            dwMaster["VAAMEGEW"] = "G  ";
            dwMaster["VAAMENGEH"] = "SQM";
            dwMaster["VAABRTVON"] = Convert.ToDouble(msg.P2) * 10;
            if (cWS != null)
            {
                dwMaster["WABIND"] = cWS.dwWS["WABIND"];
                dwMaster["LSLIEFNA"] = cWS.LSLIEFNA;
            }
            SAVEVA(dwMaster);
        }
        bool IsInitialized = false;

        public void RESET()
        {
            dwData = TBMain.NewRow();
            dwData["VAAMANDANT"] = "99";
            dwData["VAAKOLL"] = ((VVAART)MV).KOLL;
            dwData["VAAARTST"] = "9";
            dwData["VAAART"] = ((VVAART)MV).ART.Trim();
            dwData["VAABEZ"] = ((VVAART)MV).DES.Trim();
            dwData["VAADART"] = ((VVAART)MV).FARBE.Trim();
            dwData["VAAEXCL"] = IMAGEPATH;

            SResult = TMV.QVA06(dwData);
            MergeData(SResult, false, false);
        }
        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            /*if (!IsInitialized)
            {

                dwData = TBMain.NewRow();
                dwData["VAAMANDANT"] = "99";
                dwData["VAAKOLL"] = ((VVAART)MV).KOLL;
                dwData["VAAARTST"] = "9";
                dwData["VAAART"] = ((VVAART)MV).ART.Trim();
                dwData["VAABEZ"] = ((VVAART)MV).DES.Trim();
                dwData["VAADART"] = ((VVAART)MV).FARBE.Trim();
                dwData["VAAEXCL"] = IMAGEPATH;

                SResult = TMV.QVA06(dwData);
                MergeData(SResult, false);
                IsInitialized = true;
            }*/
            try
            {

                if (!IsNew)
                {
                    IsNew = true;
                    //MessageBox.Show("请先输入必要信息");    
                    RESET();
                    //KOLL = "";
                    EXART = "";
                    return;
                }
                if (!IsSelfART)
                {
                    NART = EXART;
                }
                else
                {
                    if (PRODGRP.Trim().Length == 0)
                    {
                        MessageBox.Show("请先输入产品组");
                        return;
                    }
                }
                if (NART.Trim().Length == 0)
                {
                    MessageBox.Show("请先输入品种编号");
                    return;
                }
                if (EXART.Trim().Length == 0)
                {
                    MessageBox.Show("请先输入供应商品种编号");
                    return;
                }
                if (ARTGRP.Trim().Length == 0)
                {
                    ARTGRP = "   ";
                }
                if (!IsSameProdArt)
                {
                    if (EXART.Trim().Length == 0)
                    {
                        MessageBox.Show("请先定义生产品种");
                        return;
                    }
                }
                PM["KOLL"] = KOLL;
                PM["ARTST"] = ARTST;
                PM["ART"] = NART;
                PM["VAAPRGR"] = PRODGRP;
                PM["VAAARTGRP"] = ARTGRP;
                PM["EXART"] = EXART;
                PM["DES"] = EXART;
                ((VVAART)MV).NEWVAART(PM);
                vwMaster.View.MoveCurrentToFirst();
                base.NewCommand(sender, e);
                IsNew = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            dwData = TBMain.NewRow();
            dwData["VAAMANDANT"] = app.Mandant;
            dwData["VAAKOLL"] = ((VVAART)MV).KOLL;
            dwData["VAAARTST"] = ((VVAART)MV).ARTST;
            dwData["VAAART"] = ((VVAART)MV).ART.Trim();
            dwData["VAABEZ"] = ((VVAART)MV).DES.Trim();
            dwData["VAADART"] = ((VVAART)MV).FARBE.Trim();
            dwData["VAAEXCL"] = IMAGEPATH;
            if (VAR.Trim().Length > 0)
            {
                dwData["VAAFILLER"] = " AND VAAEXCL='" + VAR + "'";
            }

            SResult = TMV.QVA06(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }


        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                DES = dwMaster["VAABEZ"].ToString();
                cWS.SaveCommand(null, null);
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "WS2VA":
                    UpdateByWS(msg);
                    break;
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataTable tb = TBMain.Clone();
            string sind = "", ART = "";

            if (dwMaster == null)
                return;
            ART = dwMaster["VAADART"].ToString();
            dwData = TMV.VWS.TBMain.NewRow();
            dwData["WAMANDANT"] = app.Mandant;
            dwData["WAART"] = ART;
            SResult = TMV.VWS.QueryWASTAMM(dwData, 12);
            Set.Merge(SResult);

            tb.TableName = "VAART";
            Set.Merge(tb);
            cDR.DocType = VALBTYP;
            cDR.PRINT(Set);
        }
        private void btnPrintVA06_Click(object sender, RoutedEventArgs e)
        {
            DataSet Set = new DataSet();
            DataTable tb = TBMain.Clone();
            string sind = "", ART = "";
            Set.Merge(TMV.SetProd);
            cDRVA06.PRINT(Set);
        }

        public override void OnQueryChg(bool value)
        {
            if (!IsQuery)
                return;
            QueryCommand(null, null);
            base.OnQueryChg(value);
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsImageMax = !IsImageMax;
        }
        private void cImage_MouseWheel(object sender, MouseWheelEventArgs e)
        {

        }

        public override void OnAppModeChg()
        {
            switch (app.company)
            {
                case "TST":
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            IsSelfART = false;
                            break;
                        case "TMODE":
                            IsSelfART = true;
                            break;

                    }
                    break;
            }

            base.OnAppModeChg();
        }
        public override void OnFBRTLChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFBRTL"] = FBRTL;
            base.OnFBRTLChg(value);
        }
        public override void OnFBRTUChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFBRTU"] = FBRTU;
            base.OnFBRTUChg(value);
        }
        public override void OnFGRQMChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFGRQM"] = FGRQM;
            base.OnFGRQMChg(value);
        }
        public override void OnFDDIMChg(string value)
        {

            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFDDIM"] = FDDIM;
            base.OnFDDIMChg(value);
        }
        public override void OnFFDDISChg(double value)
        {

            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFFDDIS"] = FFDDIS;
            base.OnFFDDISChg(value);
        }
        public override void OnFFDDIKChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFFDDIK"] = FFDDIK;
            base.OnFFDDIKChg(value);
        }

    }
}
